//
//  DateView.m
//  DatePicker
//
//  Created by 红沙尘 on 17/4/24.
//  Copyright © 2017年 红沙尘. All rights reserved.
//

#import "DateView.h"

@interface DateView ()<UIPickerViewDelegate,UIPickerViewDataSource>
{
    int curYear;
    int curMonth;
    int curDay;
    int curHour;
    int curMinte;
    
    int minYear;
    
    NSArray *colsArray;
}
@end

@implementation DateView

- (id)initWithFrame:(CGRect)frame delegate:(id <DateViewDelegate>)delegate {
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.delegate = delegate;
        
        colsArray = @[@"年",@"月",@"日",@"时",@"分"];
        
        minYear = 1901;
        
        NSDate * date = [NSDate date];//当前时间
        NSCalendar * calendar = [NSCalendar currentCalendar];//当前用户的calendar
        NSDateComponents * components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMinute | NSCalendarUnitMonth | NSCalendarUnitHour | NSCalendarUnitDay fromDate:date];
        
        curYear  = (int)components.year;
        curMonth = (int)components.month;
        curDay   = (int)components.day;
        curHour  = (int)components.hour;
        curMinte = (int)components.minute;
        
        NSLog(@"%d年%d月%d日%d时%d分" ,curYear,curMonth,curDay,curHour,curMinte);
        
        [self addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];

        CGRect rect = [UIScreen mainScreen].bounds;
        
        UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, rect.size.height,
                                                                                  rect.size.width, 216)];
        pickerView.dataSource = self;
        pickerView.delegate   = self;
        pickerView.backgroundColor = [UIColor whiteColor];
        
        [self addSubview:pickerView];
        
        [pickerView selectRow:curYear - minYear  inComponent:0 animated:NO];
        [pickerView selectRow:curMonth - 1 inComponent:1 animated:NO];
        [pickerView selectRow:curDay - 1   inComponent:2 animated:NO];
        [pickerView selectRow:curHour - 1  inComponent:3 animated:NO];
        [pickerView selectRow:curMinte - 1 inComponent:4 animated:NO];
        
        [UIView animateWithDuration:0.3 animations:^{
            
            self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
            CGRect pickerFrame = pickerView.frame;
            pickerFrame.origin.y = rect.size.height - pickerFrame.size.height;
            pickerView.frame = pickerFrame;
        }];
        
        [self resetDate];
    }
    
    return self;
}

#pragma mark - <UIPickerViewDelegate,UIPickerViewDataSource>

/***表示UIPickerVIew一共有几列**/
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    
    return colsArray.count;
}

/***返回component这一列有多少行数据**/
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    switch (component)
    {
        case 0:
            return 198;//198年
            break;
        case 1:
            return 12;//每年12个月
            break;
        case 2:
        {
            return [self getDayCountOfaMonth:curMonth];//每月天数
        }
            break;
        case 3:
            return 24;//每天24小时
            break;
        case 4:
            return 60;//每小时60分钟
            break;
        default:
            return 0;
            break;
    }
}



/*返回component列中的row行的里面的字符串**/
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    NSString *title = colsArray[component];

    if (component == 0) {
        
        return [NSString stringWithFormat:@"%d%@",(int)row + minYear,title];
        
    } else if (component == 3 || component == 4){
        
        return [NSString stringWithFormat:@"%d%@",(int)row,title];
    } else {
        
        return [NSString stringWithFormat:@"%d%@",(int)row + 1,title];
    }
    
    return @"";
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    CGFloat width = [UIScreen mainScreen].bounds.size.width;

    if (component == 0)
    {
        return 95;
    }
    
    return (width - 90 - 30)/(colsArray.count - 1);
}

- (UIView *)pickerView:(UIPickerView *)pickerView
            viewForRow:(NSInteger)row
          forComponent:(NSInteger)component
           reusingView:(UIView *)view {
    
    UILabel* pickerLabel = (UILabel*)view;
    
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.font = [UIFont systemFontOfSize:15];
        pickerLabel.textAlignment = NSTextAlignmentCenter;
    }
    
    // Fill the label text here
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}

/**选中，component列，row行的时候的回调的方法**/
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
    switch (component) {
        case 0:
        {
            curYear = (int)row + minYear;
            [pickerView reloadComponent:2];
        }
            break;
        case 1:
        {
            curMonth = (int)row + 1;
            [pickerView reloadComponent:2];
        }
            break;
        case 2:
        {
            curDay = (int)row + 1;
        }
            break;
        case 3:
        {
            curHour = (int)row;
        }
            break;
        case 4:
        {
            curMinte = (int)row;
        }
            break;
        default:
            break;
    }
    
    [self resetDate];
}

- (void)close {
    
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)resetDate {
    
    //NSLog(@"%d年%d月%d日%d时%d分" ,curYear,curMonth,curDay,curHour,curMinte);
    self.curDate = [NSString stringWithFormat:@"%d-%.2d-%.2d %.2d:%.2d",curYear,curMonth,curDay,curHour,curMinte];

    NSLog(@"%@",self.self.curDate);
    
    if ([self.delegate respondsToSelector:@selector(dateDidChanged:)]) {
        [self.delegate dateDidChanged:self.curDate];
    }
}

- (int)getDayCountOfaMonth:(int)month
{
    switch (month) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            return 31;
        case 2:
        {
            if (curYear%4==0&&curYear!=0) {
                
                return 29;
            }else{
                return 28;
            }
        }
        case 4:
        case 6:
        case 9:
        case 11:
            return 30;
        default:
            return 31;
    }
}

@end
